import { Request } from "../node_modules/utils-lib-js/dist/esm/index.js";
const request = new Request("https://api.openai.com");
const __apiKey = "sk-xxxxxxxxxxxxxxxxxxxxxx";
const messages = [];
export const __conf = {
  // max_tokens: 2048,// 回复大小限制
  model: "gpt-3.5-turbo", // 语言模型
  temperature: 0.9, // 随机回答，问题相同的前提下，值越高越随机
}; // 默认配置
// 发送消息
export async function sendMsg(prompt, conf = __conf, apiKey = __apiKey) {
  addMessages(messages, prompt);
  return request.POST(
    "/v1/chat/completions",
    {},
    { messages, ...conf },
    {
      headers: {
        "content-type": "application/json",
        Authorization: `Bearer ${apiKey}`,
      },
    }
  );
}

// 初始化需要获取的标签
export const initElem = (ids) => {
  const elems = {};
  getElemId(elems, ids);
  return elems;
};
export const elems = initElem(["send_msg", "rec_msg", "send_msg_btn"]);
// 通过id获取dom元素
function getElemId(source, ids) {
  ids.forEach((id) => (source[id] = document.querySelector(`#${id}`)));
}
export const addMessages = (msgs, content, role = "user") => {
  msgs.push({ role, content });
  return msgs;
};
